package com.journey.core.service;

import com.journey.api.user.service.IUserService;
import com.journey.core.mapper.UserMapper;
import com.journey.model.common.ResponseCode;
import com.journey.model.common.ServerResponse;
import com.journey.model.entity.User;
import com.journey.util.NoteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by ADMIN on 2018/3/15.
 */
@Service("userService")
public class UserServiceImp implements IUserService {

    @Autowired
    private UserMapper userMapper;

    public ServerResponse<String> checkLogin(String name, String pwd) {
        User user = userMapper.findByName(name);
        //检测用户名
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"用户不存在");
        }
        //检测密码
        String md5_password = NoteUtil.md5(pwd);
        if(!user.getCnUserPassword().equals(md5_password)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"密码不正确");
        }
        String cnUsrId = user.getCnUserId();
        return ServerResponse.createBySuccess(ResponseCode.SUCCESS.getCode() , "用户名和密码正确" , cnUsrId);
    }

    @Override
    public ServerResponse<User> regist(String name, String password, String nickname) {
        //检测用户名是否被占用
        User has_user = userMapper.findByName(name);
        if(has_user != null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"用户名被占用");
        }
        //注册
        User user = new User();
        user.setCnUserName(name);//设置用户名
        user.setCnUserNick(nickname);//设置昵称
        String md5_pwd = NoteUtil.md5(password);//密码md5加密处理
        user.setCnUserPassword(md5_pwd);//设置密码
        user.setCnUserId(NoteUtil.createId());//设置ID
        //调用userMapper保存
        userMapper.insertSelective(user);
        return ServerResponse.createBySuccessMessage(ResponseCode.SUCCESS.getCode(), "注册成功");
    }

    /*@Override
    public NoteResult findPassword(String userId,String pwd) {
        NoteResult result = new NoteResult();
        String md5_password = NoteUtil.md5(pwd);
        User user = userMapper.findByUserId(userId);
        //检测用户名
        if(user == null){
            result.setStatus(1);
            result.setMsg("用户名不存在!");
            return result;
        }
        if(!user.getCnUserPassword().equals(md5_password)){
            result.setStatus(2);
            result.setMsg("原始密码不正确!");
            return result;
        }
        result.setStatus(0);
        result.setMsg("原始密码正确");
        return result;
    }

    @Override
    public NoteResult updatePassword(String userId,String newPwd) {
        NoteResult result = new NoteResult();
        User user = new User();
        user.setCnUserId(userId);
        String md5_password = NoteUtil.md5(newPwd);
        user.setCnUserPassword(md5_password);
        userMapper.updatePassword(user);
        result.setStatus(0);
        result.setMsg("修改密码成功");
        result.setData(user);
        return result;
    }*/
}
